package getOffer.offer31;

import java.util.Stack;

/**
 * @Author: wjy
 * @Date: 2021/11/27 15:12
 */
public class Solution {

    public static void main(String[] args) {

        int[] pushed = {1, 2, 3, 4, 5};
        int[] popped = {4, 5, 3, 1, 2};

        Solution solution = new Solution();
        System.out.println(solution.validateStackSequences(pushed, popped));

    }

    public boolean validateStackSequences(int[] pushed, int[] popped) {

        Stack<Integer> stack = new Stack<>();

        int num = 0;

        for (int i : pushed) {
            stack.push(i);
            while (!stack.isEmpty() && stack.peek() == popped[num]) {
                stack.pop();
                num++;
            }
        }

        return stack.isEmpty();
    }

}
